package com.example.ui.share;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.huashangapp.HSApplication;
import com.example.huashangapp.R;
import com.example.util.PhotoUtil;

public class AlbumActivity extends Activity {
	Button leftBtn, rightBtn;
	TextView titleText;
	ListView listview;
	LinearLayout proBar;
	GridImageAdapter imageAdapter;
	
	Thread phoneThread;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.album);
		
		findView();
		listView();
	}

	private void findView() {
		leftBtn = (Button) findViewById(R.id.layout_title_left_btn);
		rightBtn = (Button) findViewById(R.id.layout_title_right_btn);
		titleText = (TextView) findViewById(R.id.layout_title_middle_text);
		proBar=(LinearLayout)findViewById(R.id.album_progressbar);
		listview = (ListView) findViewById(R.id.album_listview);
		init();
	}

	private void init() {
		leftBtn.setText("取消");
		rightBtn.setVisibility(View.INVISIBLE);
		titleText.setText("手机相册");
		leftBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(phoneThread.isAlive()){
					phoneThread.interrupt();//stop();//结束
				}
				finish();
			}
		});
		
		// 获取手机里的图片内容
		HSApplication.albumMap.clear();
		phoneThread = new Thread(new Runnable() {
			
			@Override
			public void run() {
				File sdRootFolder = new File("/sdcard/");
				getFiles(sdRootFolder);
				runOnUiThread(new Runnable() {
					public void run() {
						listview.setAdapter(new GridImageAdapter());
						proBar.setVisibility(View.GONE);
					}
				});
				
			}
		});
		phoneThread.start();
		
		
	}

	private void listView() {
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				//传递文件夹地址到文件夹内容显示类
				Intent intent = new Intent();
				intent.setClass(AlbumActivity.this, SelectAlbumActivity.class);
				String path = (String) HSApplication.albumMap.keySet()
						.toArray()[arg2];
				intent.putExtra("path", path);
				startActivity(intent);
			}
		});
	}

	private class GridImageAdapter extends BaseAdapter {

		public int getCount() {
			return HSApplication.albumMap.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if (convertView == null) {
				convertView = LayoutInflater.from(AlbumActivity.this).inflate(
						R.layout.album_list_item, null);
				holder = new ViewHolder();
				holder.photo = (ImageView) convertView
						.findViewById(R.id.phonealbum_item_photo);
				holder.name = (TextView) convertView
						.findViewById(R.id.phonealbum_item_name);
				holder.count = (TextView) convertView
						.findViewById(R.id.phonealbum_item_count);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			List<Map<String, String>> list = HSApplication.albumMap.get(HSApplication.albumMap
					.keySet().toArray()[position]);
			holder.photo.setImageBitmap(PhotoUtil.getImageThumbnail(list.get(0)
					.get("image_path"), 70, 70));
			holder.name.setText(list.get(0).get("image_parent_name"));
			holder.count.setText("(" + list.size() + ")");
			return convertView;
		}

		class ViewHolder {
			ImageView photo;
			TextView name;
			TextView count;
		}
	}

	/**
	 * 获取文件目录下的图片内容
	 * 
	 * @param folder
	 */
	private void getFiles(File folder) {
		File[] files = folder.listFiles();
		if (files != null) {
			for (int i = 0; i < files.length; i++) {
				File file = files[i];
				if (getImageFile(file.getName())) {
					if (HSApplication.albumMap.containsKey(folder.getAbsolutePath())) {
						List<Map<String, String>> list = HSApplication.albumMap.get(folder
								.getAbsolutePath());
						Map<String, String> map = new HashMap<String, String>();
						map.put("image_name", file.getName());
						map.put("image_path", file.getAbsolutePath());
						map.put("image_parent_name", folder.getName());
						map.put("image_parent_path", folder.getAbsolutePath());
						list.add(map);
					} else {
						List<Map<String, String>> list = new ArrayList<Map<String, String>>();
						Map<String, String> map = new HashMap<String, String>();
						map.put("image_name", file.getName());
						map.put("image_path", file.getAbsolutePath());
						map.put("image_parent_name", folder.getName());
						map.put("image_parent_path", folder.getAbsolutePath());
						list.add(map);
						HSApplication.albumMap.put(folder.getAbsolutePath(), list);
					}
				}
				if (file.isDirectory()) {
					getFiles(file);
				}
			}
		}

	}

	/**
	 * 判断是否为图片
	 * 
	 * @param fName
	 *            文件的名字
	 * @return
	 */
	private boolean getImageFile(String fName) {
		boolean re;

		String end = fName
				.substring(fName.lastIndexOf(".") + 1, fName.length())
				.toLowerCase();

		if (end.equals("jpg") || end.equals("gif") || end.equals("png")
				|| end.equals("jpeg") || end.equals("bmp")) {
			re = true;
		} else {
			re = false;
		}
		return re;
	}

}
